package com.jiashihui.schema_lgorithm.schema.iterator;

/**
 * @author jiashihui
 * @describe: 迭代器实现类
 * @date 2019/03/15
 */
public class IteratorImpl implements Iterator {

    /* 集合接口 */
    private Collection collection;

    /* 索引位置 */
    private int pos = -1;

    public IteratorImpl(Collection collection) {
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > 0) {
            pos--;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < (collection.size() - 1)) {
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        if (pos < (collection.size() - 1)) {
            return true;
        }
        return false;
    }

    @Override
    public Object first() {
        pos = 0;
        return collection.get(pos);
    }
}
